﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;

using System.Net.Http;
//using System.Runtime.Serialization.Json;

namespace EdexClient
{
    public class EdexContext
    {
        private string uri { get; set; }

        public EdexContext(string uri)
        {
            this.uri = uri;
        }

        public void SetSession(string USER_KEY, string COMPANY_KEY)
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                var _uri = this.uri + String.Format(
                        "Security/SetActiveCompany/?USER_GUID={0}&COMPANY_GUID={1}",
                        Uri.EscapeDataString(USER_KEY),
                        Uri.EscapeDataString(COMPANY_KEY));

                var result = client.GetAsync(_uri).Result.Content;
            }
        }



        public String GetIncomingInvoicesCommerceML(string APIKEY)
        {     
            var _uri = this.uri + String.Format("/api/IncomingInvoices/?api_key={0}", APIKEY);
            
            var contentType = "application/commerceML";
            return this.Download(_uri, contentType);
        }


        private String Download(string uri, string contentType)
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                if (!String.IsNullOrEmpty(contentType))
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(contentType));

                var result = client.GetAsync(uri).Result;

                if (result.StatusCode != System.Net.HttpStatusCode.OK)
                    throw new Exception(result.Content.ReadAsStringAsync().Result);

                return result.Content.ReadAsStringAsync().Result;
                    
            }
        }


        public API_SESSION GetSession(string email, string password)
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                var uri = System.IO.Path.Combine(this.uri, "security/login/?email={0}&password={1}");
                var result = client.GetAsync(String.Format(uri, email, password)).Result;

                if (result.StatusCode != System.Net.HttpStatusCode.OK)
                    throw new Exception(result.Content.ReadAsStringAsync().Result);

                var js = new System.Web.Script.Serialization.JavaScriptSerializer();
                return js.Deserialize<API_SESSION>(result.Content.ReadAsStringAsync().Result);
            }
        }

        public void ChangeCompany(string company_key, string APIKEY)
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                var uri = System.IO.Path.Combine(this.uri, "security/ChangeCompany/?api_key={0}&company_key={1}");
                var result = client.GetAsync(String.Format(uri, APIKEY, company_key)).Result;

                if (result.StatusCode != System.Net.HttpStatusCode.OK)
                    throw new Exception(result.Content.ReadAsStringAsync().Result);
            }
        }



        public void SendItems(IEnumerable<Interfaces.IItem> data, string APIKEY)
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                var _uri = this.uri + "api/WH_ITEMS/?api_key={0}";
                _uri = String.Format(_uri, APIKEY);
                var total = data.Count();


                foreach (var record in data)
                {
                    try
                    {
                        var result = client
                            .PostAsJsonAsync<WH_ITEMS>(_uri, new WH_ITEMS
                            {
                                EXT_KEY = record.ID_Record.ToString(),
                                ITEM_NAME = record.ItemName                               
                            })
                            .Result
                            .Content;
                    }
                    catch (Exception ex) { }
                }   
            }
        }



        public void SetIncomingNumber(Guid GUID_RECORD, string DOC_ID, string APIKEY)
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                var _uri = this.uri + "api/IncomingInvoices/?api_key={0}";
                _uri = String.Format(_uri, APIKEY);
                
                try
                {
                    var result = client
                        .PostAsJsonAsync<IncomingInvoices>(_uri, new IncomingInvoices
                        {
                            GUID_RECORD = GUID_RECORD,
                            DOC_ID = DOC_ID
                        })
                        .Result
                        .Content;
                }
                catch (Exception ex) { }                
            }
        }



        public void SetIncomingNumber(Guid id, string number)
        {
            var _uri = this.uri + "api/INVOICES/SetIncomingNumber/?id={0}&number={1}";
            _uri = String.Format(_uri, id, number);

            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                try
                {
                    var result = client
                        .GetAsync(_uri)
                        .Result
                        .Content;
                }
                catch (Exception ex)
                {

                }
            }
        }


        public void SendLocations(IEnumerable<Interfaces.ILocation> data, string APIKEY)
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                var _uri = this.uri + "api/LOCATIONS/?api_key={0}";
                _uri = String.Format(_uri, APIKEY);
                var total = data.Count();

                foreach (var record in data)
                {
                    try
                    {
                        var result = client
                            .PostAsJsonAsync<WH_LOCATIONS>(_uri, new WH_LOCATIONS
                            {
                                EXT_KEY = record.ID_Record.ToString(),
                                LOCATION_NAME = record.LocationName,
                                STREET = record.Address,
                                IS_DEFAULT = false
                            })
                            .Result
                            .Content;
                    }
                    catch (Exception ex) { }    
                }
            }
        }



        public void SendSupplier(IEnumerable<Interfaces.ISupplier> data, string APIKEY)
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                var _uri = this.uri + "api/WH_COMPANIES/?api_key={0}";
                _uri = String.Format(_uri, APIKEY);
                var total = data.Count();

                foreach (var record in data)
                {
                    try
                    {
                        var result = client
                            .PostAsJsonAsync<WH_COMPANIES>(_uri, new WH_COMPANIES
                            {
                                EXT_KEY = record.ID_Record.ToString(),
                                COMPANY_NAME = record.SupplierName
                            })
                            .Result
                            .Content;
                    }
                    catch (Exception ex) { }
                }
            }
        }



    }
}
